package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.NewsContent;
import cn.wolfcode.trip.base.domain.News;
import cn.wolfcode.trip.base.query.NewsQueryObject;
import cn.wolfcode.trip.base.service.INewsService;
import cn.wolfcode.trip.base.util.JsonResult;
import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/news")
public class NewsController {
    @Autowired
    private INewsService newsService;

    @RequestMapping("list")
    public String list(@ModelAttribute("qo") NewsQueryObject qo, Model model) {
        model.addAttribute("pageInfo", newsService.query(qo));
        return "news/list";
    }

    @RequestMapping("/getContentById")
    @ResponseBody
    public NewsContent getNewsContentById(Long id) {
        return newsService.getNewsContentById(id);
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(News news, MultipartFile file) {
        if (file != null && file.getSize() > 0) {
            String url = UploadUtil.uploadQiniu(file);
            news.setCoverUrl(url);
        }
        newsService.saveOrUpdate(news);
        return new JsonResult();
    }
}
